import _ from '../../../utils/under-dash';
import BaseXform from '../base-xform';

class PageMarginsXform extends BaseXform {
    get tag() {
        return 'pageMargins';
    }

    render(xmlStream, model) {
        if (model) {
            const attributes = {
                left: model.left,
                right: model.right,
                top: model.top,
                bottom: model.bottom,
                header: model.header,
                footer: model.footer,
            };
            if (_.some(attributes, value => value !== undefined)) {
                xmlStream.leafNode(this.tag, attributes);
            }
        }
    }

    parseXmlObject(node) {
        if (node._name === this.tag) {
            this.model = {
                left: parseFloat(node._attributes.left || 0.7),
                right: parseFloat(node._attributes.right || 0.7),
                top: parseFloat(node._attributes.top || 0.75),
                bottom: parseFloat(node._attributes.bottom || 0.75),
                header: parseFloat(node._attributes.header || 0.3),
                footer: parseFloat(node._attributes.footer || 0.3),
            };
            return true;
        }
        return false;
    }

    parseOpen(node) {
        switch (node.name) {
            case this.tag:
                this.model = {
                    left: parseFloat(node.attributes.left || 0.7),
                    right: parseFloat(node.attributes.right || 0.7),
                    top: parseFloat(node.attributes.top || 0.75),
                    bottom: parseFloat(node.attributes.bottom || 0.75),
                    header: parseFloat(node.attributes.header || 0.3),
                    footer: parseFloat(node.attributes.footer || 0.3),
                };
                return true;
            default:
                return false;
        }
    }

    parseText() {
    }

    parseClose() {
        return false;
    }
}

export default PageMarginsXform;
